package io.renren.modules.xsl.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.renren.modules.oatools.utils.http.HttpRequestSend;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

import static io.renren.modules.oatools.utils.http.OAHttpRequestUtils.requestSend;

public class ConnectTools {
    private final static Logger logger = LoggerFactory.getLogger(ConnectTools.class);

    /**
     * 外网地址
     */
//    @Value("${xslSuite.out-target-url}")  // 对应 yml 中的 myapp.name
    private static String outTargetUrl ="http://119.39.117.176:18080";

    /**
     * 获取token
     *
     * @return
     */
    public static String getDevToken() {
        HttpRequestSend httpRequestToke = new HttpRequestSend();
        httpRequestToke.setRequestUrl(outTargetUrl + "/open-api/token/getDevToken");
        httpRequestToke.setRequestType("POST");
        httpRequestToke.setRequestBody("{\"apiKey\":\"b2cedf7359aa2a4d\"}");
        try {
            Map<String, Object> resDeptPersons = requestSend(null, httpRequestToke);
            JSONObject jsonObject = JSONObject.parseObject(resDeptPersons.get("mag").toString());
            JSONObject data = jsonObject.getJSONObject("data");
            return data.getString("devToken");
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            logger.info("获取薪事力token");
        }
    }

    public static void main(String[] args) throws IOException {
//        System.out.println(getDevToken());
/*

        String userId = AesUtil.encode("16673236249", getDevToken());
        //获取部门信息
        HttpRequestSend httpRequestSend = new HttpRequestSend();
        httpRequestSend.setRequestUrl(outTargetUrl + "/open-api/group/findCompanyList?includeSelf=true&dev_login_type=PHONE&user_id=16673236249");
        httpRequestSend.setRequestType("GET");
        httpRequestSend.setRequestHeaders("{devToken:\"" + getDevToken() + "\"}");

        Map<String, Object> res = null;
        try {
            res = requestSend(null, httpRequestSend);
            System.out.println(JSON.toJSONString(res));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
*/
        HttpRequestSend httpRequestSend = new HttpRequestSend();
        httpRequestSend.setRequestUrl(outTargetUrl + "/open-api/department/findAll");
        httpRequestSend.setRequestType("GET");
        httpRequestSend.setRequestHeaders("{devToken:\"" + getDevToken() + "\"}");
        Map<String, Object> res = requestSend(null, httpRequestSend);
        JSONObject jsonObject = JSONObject.parseObject(res.get("mag").toString());
        JSONArray data = jsonObject.getJSONArray("data");
        //组织信息
        JSONArray organizeArray = new JSONArray();
        for (int i = 0; i < data.size(); i++) {
            JSONObject object = (JSONObject) data.get(i);
            if (object.get("parentId").equals("5628262272371569605")){
                organizeArray.add(object);
            }
        }
        
        // 输出原始JSON数据
        System.out.println("原始JSON数据：");
        System.out.println(organizeArray.toString());
        
        // 转换为XML格式
        System.out.println("\n转换后的XML数据：");
        String xmlResult = XslXmlConverter.convertOrganizationToXml(organizeArray);
        System.out.println(xmlResult);
    }
}